package logic

import (
	"context"

	"yookie.me/pdfreader/internal/svc"
	"yookie.me/pdfreader/internal/types"
	"yookie.me/pdfreader/thirdparty/upload"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadGetLogic struct {
	logx.Logger
	ctx           context.Context
	svcCtx        *svc.ServiceContext
	uploadHandler upload.IUploader
}

func NewUploadGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadGetLogic {
	uploader, err := upload.NewOssUploader(&upload.OssConf{
		AccessKeyID:     svcCtx.Config.OssConf.AccessKeyID,
		AccessKeySecret: svcCtx.Config.OssConf.AccessKeySecret,
		EndPoint:        svcCtx.Config.OssConf.EndPoint,
		Bucket:          svcCtx.Config.OssConf.Bucket,
	})
	if err != nil {
		panic(err)
	}
	return &UploadGetLogic{
		Logger:        logx.WithContext(ctx),
		ctx:           ctx,
		svcCtx:        svcCtx,
		uploadHandler: uploader,
	}
}

func (l *UploadGetLogic) UploadGet(req *types.UploadGetRequest) (resp *types.UploadGetResponse, err error) {
	url, err := l.uploadHandler.GetUrl(req.Uri, 600)
	if err != nil {
		return nil, err
	}

	return &types.UploadGetResponse{
		Url: url,
	}, nil
}
